﻿using Core.Framework;
using DotNetty.Buffers;
using DotNetty.Codecs.Http.WebSockets;
using DotNetty.Transport.Channels;
using DotNetty.Transport.Channels.Groups;
using System.Text;
using System.Threading.Tasks;

namespace DotNetty.Framework
{
    public static class ExtendChannelWrite
    {
        /// <summary>
        /// 推送消息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="message">消息内容</param>
        public static async Task WriteAndFlushAsyncOfTextAsync(this IChannelHandlerContext context, string message)
        {
            var textOfResult = new TextWebSocketFrame(message);
            await context.Channel.WriteAndFlushAsync(textOfResult);
        }

        /// <summary>
        /// 推送消息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="message">消息内容</param>
        public static async Task WriteAndFlushAsyncOfTextAsync<T>(this IChannelHandlerContext context, T message)
            where T : class
        {
            string messageStr = message.TryToJson();
            var textOfResult = new TextWebSocketFrame(messageStr);
            await context.Channel.WriteAndFlushAsync(textOfResult);
        }


        /// <summary>
        /// 推送消息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="message">消息内容</param>
        public static async Task WriteAndFlushAsyncOfTextAsync(this IChannelGroup context, string message)
        {
            var textOfResult = new TextWebSocketFrame(message);
            await context?.WriteAndFlushAsync(textOfResult);
        }

        /// <summary>
        /// 推送消息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="message">消息内容</param>
        public static async Task WriteAndFlushAsyncOfTextAsync<T>(this IChannelGroup context, T message)
            where T : class
        {

            string messageStr = message.TryToJson();
            var textOfResult = new TextWebSocketFrame(messageStr);
            await context?.WriteAndFlushAsync(textOfResult);
        }

        #region Binary
        /// <summary>
        /// 推送消息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="message">消息内容</param>
        public static async Task WriteAndFlushAsyncOfBinaryAsync(this IChannelHandlerContext context, string message)
        {
            var byteBuffer = Unpooled.WrappedBuffer(Encoding.UTF8.GetBytes(message));
            var binaryOfResult = new BinaryWebSocketFrame(byteBuffer);
            await context.Channel.WriteAndFlushAsync(binaryOfResult);
        }

        /// <summary>
        /// 推送消息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="message">消息内容</param>
        public static async Task WriteAndFlushAsyncOfBinaryAsync<T>(this IChannelHandlerContext context, T message)
            where T : class
        {
            string messageStr = message.TryToJson();
            var byteBuffer = Unpooled.WrappedBuffer(Encoding.UTF8.GetBytes(messageStr));
            var binaryOfResult = new BinaryWebSocketFrame(byteBuffer);
            await context?.Channel.WriteAndFlushAsync(binaryOfResult);
        }


        /// <summary>
        /// 推送消息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="message">消息内容</param>
        public static async Task WriteAndFlushAsyncOfBinaryAsync(this IChannelGroup context, string message)
        {
            var byteBuffer = Unpooled.WrappedBuffer(Encoding.UTF8.GetBytes(message));
            var binaryOfResult = new BinaryWebSocketFrame(byteBuffer);
            await context?.WriteAndFlushAsync(binaryOfResult);
        }

        /// <summary>
        /// 推送消息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="message">消息内容</param>
        public static async Task WriteAndFlushAsyncOfBinaryAsync<T>(this IChannelGroup context, T message)
            where T : class
        {
            string messageStr = message.TryToJson();
            var byteBuffer = Unpooled.WrappedBuffer(Encoding.UTF8.GetBytes(messageStr));
            var binaryOfResult = new BinaryWebSocketFrame(byteBuffer);
            await context?.WriteAndFlushAsync(binaryOfResult);
        }
        #endregion
    }
}
